/**
 * Created by apple on 2018/2/23.
 */
requireJS(['constant', 'site', 'handlebars'], function (Constant, site, Handlebars) {

    function getRoleId(str, aim) {
        var reg = new RegExp("(^|&)" + aim + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var index = str.lastIndexOf("?");
        str = str.substring(index + 1, str.length);
        var r = str.match(reg);
        if (r != null) return r[2];
        return null; //返回参数值
    }


    var url = {
        consoleStatistic: "manager/consoleStatistic.do",
        myOperationStatistic: "manager/myOperationStatistic.do"
    }
    var menus = JSON.parse(localStorage.getItem('content')).menus;

    Constant.initHandlebars(Handlebars);


    Handlebars.registerHelper('if_eq', function (v1, v2, opts) {
        if (v1 == v2)
            return opts.fn(this);
        else
            return opts.inverse(this);
    });

    var menus = JSON.parse(localStorage.getItem('content')).menus;

    $.each(menus,function(index,item){
        if(item.key === 'oneData'){
            $('.more_data').show();
        }else{
            $('.more_data').hide();
        }
    })

    $(".more_data").click(function () {
        pjaxGo('data/customer_count.html?h=9&i=0')
    })

    $(".department").html(JSON.parse(localStorage.getItem('content')).admin.departmentName + " > " + JSON.parse(localStorage.getItem('content')).admin.roleName)

    site.send({
        url: url.myOperationStatistic,
        callback: function (data) {
            data = JSON.parse(data);
            if (data.type == 1) {
                if (data.content[0]) {
                    $.each(data.content, function (i, e) {
                        $.each(menus, function (ii, first) {
                            if (e.key == 'applyOrder' || e.key == 'appliedOrder') {
                                if (e.key == first.key && JSON.parse(localStorage.getItem('content')).admin.roleid == getRoleId(first.url, 'roleId')) {
                                    var url;
                                    if (first.children[0]) {
                                        if (first.children[0].children[0]) {
                                            url = first.children[0].children[0].url
                                        }
                                        else {
                                            url = first.children[0].url;
                                        }
                                    }
                                    else {
                                        url = first.url;
                                    }
                                    if (url.indexOf('?') > -1) {
                                        $("#special_list").append('<li data-url=' + url + '&h=' + ii + '&i=0&t=0>\n' +
                                            '            <span class="contrlSLeft" ng-click="F.changeHas(\'informa\',\'liuyan\')"><i\n' +
                                            '            class="iconfont icon-message"></i></span>\n' +
                                            '            <span class="contrlSRight" ng-click="F.changeHas(\'informa\',\'\')">\n' +
                                            '            <div>' + e.name + '</div>\n' +
                                            '            <!--<div>77777</div>-->\n' +
                                            '            </span>\n' +
                                            '            </li>')
                                    }
                                    else {
                                        $("#special_list").append('<li data-url=' + url + '?h=' + ii + '&i=0&t=0>\n' +
                                            '            <span class="contrlSLeft" ng-click="F.changeHas(\'informa\',\'liuyan\')"><i\n' +
                                            '            class="iconfont icon-message"></i></span>\n' +
                                            '            <span class="contrlSRight" ng-click="F.changeHas(\'informa\',\'\')">\n' +
                                            '            <div>' + e.name + '</div>\n' +
                                            '            <!--<div>77777</div>-->\n' +
                                            '            </span>\n' +
                                            '            </li>')
                                    }
                                }
                            }
                            else if (e.key == 'twoOrderIn') {
                                if (e.key == first.key) {
                                    var url = 'loanBefore/product_orderIn.html';
                                    /*if (first.children[0]) {
                                        if (first.children[0].children[0]) {
                                            url = first.children[0].children[0].url
                                        }
                                        else {
                                            url = first.children[0].url;
                                        }
                                    }
                                    else {
                                        url = first.url;
                                    }*/
                                    if (url.indexOf('?') > -1) {
                                        $("#special_list").append('<li data-url=' + url + '&h=' + ii + '&i=0&t=0>\n' +
                                            '            <span class="contrlSLeft" ng-click="F.changeHas(\'informa\',\'liuyan\')"><i\n' +
                                            '            class="iconfont icon-' + e.key + '"></i></span>\n' +
                                            '            <span class="contrlSRight" ng-click="F.changeHas(\'informa\',\'\')">\n' +
                                            '            <div>' + e.name + '</div>\n' +
                                            '            <!--<div>77777</div>-->\n' +
                                            '            </span>\n' +
                                            '            </li>'
                                        )
                                    }
                                    else {
                                        $("#special_list").append('<li data-url=' + url + '&h=' + ii + '&i=0&t=0>\n' +
                                            '            <span class="contrlSLeft" ng-click="F.changeHas(\'informa\',\'liuyan\')"><i\n' +
                                            '            class="iconfont icon-' + e.key + '"></i></span>\n' +
                                            '            <span class="contrlSRight" ng-click="F.changeHas(\'informa\',\'\')">\n' +
                                            '            <div>' + e.name + '</div>\n' +
                                            '            <!--<div>77777</div>-->\n' +
                                            '            </span>\n' +
                                            '            </li>')
                                    }
                                }
                            }
                            else if (e.key == first.key) {
                                var url;
                                if (first.children[0]) {
                                    if (first.children[0].children[0]) {
                                        url = first.children[0].children[0].url
                                    }
                                    else {
                                        url = first.children[0].url;
                                    }
                                }
                                else {
                                    url = first.url;
                                }
                                if (url.indexOf('?') > -1) {
                                    $("#special_list").append('<li data-url=' + url + '&h=' + ii + '&i=0&t=0>\n' +
                                        '            <span class="contrlSLeft" ng-click="F.changeHas(\'informa\',\'liuyan\')"><i\n' +
                                        '            class="iconfont icon-' + e.key + '"></i></span>\n' +
                                        '            <span class="contrlSRight" ng-click="F.changeHas(\'informa\',\'\')">\n' +
                                        '            <div>' + e.name + '</div>\n' +
                                        '            <!--<div>77777</div>-->\n' +
                                        '            </span>\n' +
                                        '            </li>'
                                    )
                                }
                                else {
                                    $("#special_list").append('<li data-url=' + url + '?h=' + ii + '&i=0&t=0>\n' +
                                        '            <span class="contrlSLeft" ng-click="F.changeHas(\'informa\',\'liuyan\')"><i\n' +
                                        '            class="iconfont icon-' + e.key + '"></i></span>\n' +
                                        '            <span class="contrlSRight" ng-click="F.changeHas(\'informa\',\'\')">\n' +
                                        '            <div>' + e.name + '</div>\n' +
                                        '            <!--<div>77777</div>-->\n' +
                                        '            </span>\n' +
                                        '            </li>')
                                }
                            }
                            $.each(first.children, function (iii, second) {
                                if (e.key == 'applyOrder' || e.key == 'appliedOrder') {
                                    if (e.key == second.key && JSON.parse(localStorage.getItem('content')).admin.roleid == getRoleId(second.url, 'roleId')) {
                                        var url;
                                        if (first.children[0]) {
                                            if (first.children[0].children[0]) {
                                                url = first.children[0].children[0].url
                                            }
                                            else {
                                                url = first.children[0].url;
                                            }
                                        }
                                        else {
                                            url = first.url;
                                        }
                                        if (url.indexOf('?') > -1) {
                                            $("#special_list").append('<li data-url=' + url + '&h=' + ii + '&i=' + iii + '&t=0>\n' +
                                                '            <span class="contrlSLeft" ng-click="F.changeHas(\'informa\',\'liuyan\')"><i\n' +
                                                '            class="iconfont icon-' + e.key + '"></i></span>\n' +
                                                '            <span class="contrlSRight" ng-click="F.changeHas(\'informa\',\'\')">\n' +
                                                '            <div>' + e.name + '</div>\n' +
                                                '            <!--<div>77777</div>-->\n' +
                                                '            </span>\n' +
                                                '            </li>')
                                        }
                                        else {
                                            $("#special_list").append('<li data-url=' + url + '?h=' + ii + '&i=' + iii + '&t=0>\n' +
                                                '            <span class="contrlSLeft" ng-click="F.changeHas(\'informa\',\'liuyan\')"><i\n' +
                                                '            class="iconfont icon-' + e.key + '"></i></span>\n' +
                                                '            <span class="contrlSRight" ng-click="F.changeHas(\'informa\',\'\')">\n' +
                                                '            <div>' + e.name + '</div>\n' +
                                                '            <!--<div>77777</div>-->\n' +
                                                '            </span>\n' +
                                                '            </li>')
                                        }
                                    }
                                }
                                else if (e.key == 'twoOrderIn') {
                                    if (e.key == second.key) {
                                        var url = 'loanBefore/product_orderIn.html';
                                        /*if (second.children[0]) {
                                            url = second.children[0].url
                                        }
                                        else {
                                            url = second.url;
                                        }*/

                                        if (url.indexOf('?') > -1) {
                                            $("#special_list").append('<li data-url=' + url + '&h=' + ii + '&i=' + iii + '&t=0>\n' +
                                                '            <span class="contrlSLeft" ng-click="F.changeHas(\'informa\',\'liuyan\')"><i\n' +
                                                '            class="iconfont icon-' + e.key + '"></i></span>\n' +
                                                '            <span class="contrlSRight" ng-click="F.changeHas(\'informa\',\'\')">\n' +
                                                '            <div>' + e.name + '</div>\n' +
                                                '            <!--<div>77777</div>-->\n' +
                                                '            </span>\n' +
                                                '            </li>')
                                        }
                                        else {
                                            $("#special_list").append('<li data-url=' + url + '?h=' + ii + '&i=' + iii + '&t=0>\n' +
                                                '            <span class="contrlSLeft" ng-click="F.changeHas(\'informa\',\'liuyan\')"><i\n' +
                                                '            class="iconfont icon-' + e.key + '"></i></span>\n' +
                                                '            <span class="contrlSRight" ng-click="F.changeHas(\'informa\',\'\')">\n' +
                                                '            <div>' + e.name + '</div>\n' +
                                                '            <!--<div>77777</div>-->\n' +
                                                '            </span>\n' +
                                                '            </li>')
                                        }
                                    }
                                }
                                else if (e.key == second.key) {
                                    var url;
                                    if (second.children[0]) {
                                        url = second.children[0].url
                                    }
                                    else {
                                        url = second.url;
                                    }

                                    if (url.indexOf('?') > -1) {
                                        $("#special_list").append('<li data-url=' + url + '&h=' + ii + '&i=' + iii + '&t=0>\n' +
                                            '            <span class="contrlSLeft" ng-click="F.changeHas(\'informa\',\'liuyan\')"><i\n' +
                                            '            class="iconfont icon-' + e.key + '"></i></span>\n' +
                                            '            <span class="contrlSRight" ng-click="F.changeHas(\'informa\',\'\')">\n' +
                                            '            <div>' + e.name + '</div>\n' +
                                            '            <!--<div>77777</div>-->\n' +
                                            '            </span>\n' +
                                            '            </li>')
                                    }
                                    else {
                                        $("#special_list").append('<li data-url=' + url + '?h=' + ii + '&i=' + iii + '&t=0>\n' +
                                            '            <span class="contrlSLeft" ng-click="F.changeHas(\'informa\',\'liuyan\')"><i\n' +
                                            '            class="iconfont icon-' + e.key + '"></i></span>\n' +
                                            '            <span class="contrlSRight" ng-click="F.changeHas(\'informa\',\'\')">\n' +
                                            '            <div>' + e.name + '</div>\n' +
                                            '            <!--<div>77777</div>-->\n' +
                                            '            </span>\n' +
                                            '            </li>')
                                    }
                                }
                                $.each(second.children, function (iiii, third) {
                                    if (e.key == 'applyOrder' || e.key == 'appliedOrder') {
                                        if (e.key == third.key && JSON.parse(localStorage.getItem('content')).admin.roleid == getRoleId(third.url, 'roleId')) {
                                            var url = third.url;
                                            if (url.indexOf('?') > -1) {
                                                $("#special_list").append('<li data-url=' + url + '&h=' + ii + '&i=' + iii + '&t=' + iiii + '>\n' +
                                                    '            <span class="contrlSLeft" ng-click="F.changeHas(\'informa\',\'liuyan\')"><i\n' +
                                                    '            class="iconfont icon-' + e.key + '"></i></span>\n' +
                                                    '            <span class="contrlSRight" ng-click="F.changeHas(\'informa\',\'\')">\n' +
                                                    '            <div>' + e.name + '</div>\n' +
                                                    '            <!--<div>77777</div>-->\n' +
                                                    '            </span>\n' +
                                                    '            </li>')
                                            }
                                            else {
                                                $("#special_list").append('<li data-url=' + url + '?h=' + ii + '&i=' + iii + '&t=' + iiii + '>\n' +
                                                    '            <span class="contrlSLeft" ng-click="F.changeHas(\'informa\',\'liuyan\')"><i\n' +
                                                    '            class="iconfont icon-' + e.key + '"></i></span>\n' +
                                                    '            <span class="contrlSRight" ng-click="F.changeHas(\'informa\',\'\')">\n' +
                                                    '            <div>' + e.name + '</div>\n' +
                                                    '            <!--<div>77777</div>-->\n' +
                                                    '            </span>\n' +
                                                    '            </li>')
                                            }
                                        }
                                    }
                                    else if (e.key == 'twoOrderIn') {
                                        if (e.key == third.key) {
                                            var url = 'loanBefore/product_orderIn.html';
                                            if (url.indexOf('?') > -1) {
                                                $("#special_list").append('<li data-url=' + url + '&h=' + ii + '&i=' + iii + '&t=' + iiii + '">\n' +
                                                    '            <span class="contrlSLeft" ng-click="F.changeHas(\'informa\',\'liuyan\')"><i\n' +
                                                    '            class="iconfont icon-' + e.key + '"></i></span>\n' +
                                                    '            <span class="contrlSRight" ng-click="F.changeHas(\'informa\',\'\')">\n' +
                                                    '            <div>' + e.name + '</div>\n' +
                                                    '            <!--<div>77777</div>-->\n' +
                                                    '            </span>\n' +
                                                    '            </li>')
                                            }
                                            else {
                                                $("#special_list").append('<li data-url=' + url + '?h=' + ii + '&i=' + iii + '&t=' + iiii + '">\n' +
                                                    '            <span class="contrlSLeft" ng-click="F.changeHas(\'informa\',\'liuyan\')"><i\n' +
                                                    '            class="iconfont icon-' + e.key + '"></i></span>\n' +
                                                    '            <span class="contrlSRight" ng-click="F.changeHas(\'informa\',\'\')">\n' +
                                                    '            <div>' + e.name + '</div>\n' +
                                                    '            <!--<div>77777</div>-->\n' +
                                                    '            </span>\n' +
                                                    '            </li>')
                                            }
                                        }
                                    }
                                    else if (e.key == third.key) {
                                        var url = third.url;
                                        if (url.indexOf('?') > -1) {
                                            $("#special_list").append('<li data-url=' + url + '&h=' + ii + '&i=' + iii + '&t=' + iiii + '>\n' +
                                                '            <span class="contrlSLeft" ng-click="F.changeHas(\'informa\',\'liuyan\')"><i\n' +
                                                '            class="iconfont icon-' + e.key + '"></i></span>\n' +
                                                '            <span class="contrlSRight" ng-click="F.changeHas(\'informa\',\'\')">\n' +
                                                '            <div>' + e.name + '</div>\n' +
                                                '            <!--<div>77777</div>-->\n' +
                                                '            </span>\n' +
                                                '            </li>')
                                        }
                                        else {
                                            $("#special_list").append('<li data-url=' + url + '?h=' + ii + '&i=' + iii + '&t=' + iiii + '>\n' +
                                                '            <span class="contrlSLeft" ng-click="F.changeHas(\'informa\',\'liuyan\')"><i\n' +
                                                '            class="iconfont icon-' + e.key + '"></i></span>\n' +
                                                '            <span class="contrlSRight" ng-click="F.changeHas(\'informa\',\'\')">\n' +
                                                '            <div>' + e.name + '</div>\n' +
                                                '            <!--<div>77777</div>-->\n' +
                                                '            </span>\n' +
                                                '            </li>')
                                        }
                                    }
                                })
                            })
                        })
                    })
                }

                var last = $('.contrl-ul li').length % 4;
                for (var i = 1; i <= last; i++) {
                    $('.contrl-ul li').eq('-' + i).css('border-bottom', 'none');
                }

                //点击跳转
                $(".contrl-ul li").click(function () {
                    var url = $(this).data('url');
                    pjaxGo(url)
                })

                if ($("#special_list > li").length > 4) {
                    $("#special_list").css({
                        height: '307px'
                    })
                    $('.carousel').show();
                    $('.carousel').on('click', function () {
                        if ($("#special_list").css('height') === '307px') {
                            $("#special_list").css({
                                height: 'auto'
                            })
                            $(this).css({
                                transform: 'rotate(-90deg)'
                            })
                        } else {
                            $("#special_list").css({
                                height: '307px'
                            })
                            $(this).css({
                                transform: 'rotate(90deg)'
                            })
                        }
                    })
                }
            }
            else {
                Constant.fn.Message("F", data.msg)
            }
        },
    })

    site.send({
        url: url.consoleStatistic,
        callback: function (data) {
            data = JSON.parse(data);
            if (data.type == 1) {
                var source = $("#console_list_tpl").html();
                var template = Handlebars.compile(source);
                var html = template(data.content);
                $('#console_list').append(html);
            }
            else {
                Constant.fn.Message("F", data.msg)
            }
        }
    })


});